package com.experience.yledu.ticket;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class sellTicket implements Runnable{

	public static void main(String[] args) {

		sellTicket st = new sellTicket();
		Thread t1 = new Thread(st, "窗口1");
		Thread t2 = new Thread(st, "窗口2");
		Thread t3 = new Thread(st, "窗口3");
		Thread t4 = new Thread(st, "窗口4");
		Thread t5 = new Thread(st, "窗口5");
		Thread t6 = new Thread(st, "窗口6");
		Thread t7 = new Thread(st, "窗口7");

		t1.start();
		t2.start();
		t3.start();
		t4.start();
		t5.start();
		t6.start();
		t7.start();

	}


	/**
	 * 卖票
	 */
	private static int ticket = 10;

	private Lock lock = new ReentrantLock();

	@Override
	public void run(){
		while (true){
			try {
				lock.lock();
				if(ticket>0){
					System.out.println(Thread.currentThread().getName()+"正在出售第"+(ticket--)+"张票！");
				}else{
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				lock.unlock();
			}

		}
	}
}
